package com.example.shell;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Point;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.util.DisplayMetrics;

import org.chromium.content_shell.Shell;
import org.chromium.media.MediaPlayerBridge;
import org.chromium.WebView;

public class WebActivity extends Activity implements WebView.StartupCallback,
       MediaPlayerBridge.PlayerBridgeCallback {
  private static final String TAG = "WebActivity";
  private String mStartupUrl;

  private SurfaceView mPlayerView = null;
  private WebView web = null;
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    WebView.Init(this.getApplication());
    MediaPlayerBridge.setCallback(this);
    web = new WebView(this, this);
    mStartupUrl = getUrlFromIntent(getIntent());

    //WebView相当于FrameLayout
    setContentView(web);
  }

  @Override
  public MediaPlayer createPlayer() {
    return new WebMediaPlayer(mPlayerView);
  }

  @Override
  public void onChangeRect(int left, int top, int width, int height) {
    Log.i(TAG, "onChangeRect:"+left+","+top+","+width+","+height);
  }

  @Override
  public void onSuccess() {
    web.loadUrl(mStartupUrl);
  }

  @Override
  public void onFailure() {
    Log.e(TAG, "ContentView initialization failed.");
    Toast.makeText(this, "ContentView initialization failed.", Toast.LENGTH_SHORT).show();
    finish();
  }

  @Override
  public void onCreatedShell(Shell shell) {
    new WebObserver(shell.getWebContents());
  }

  @Override
  protected void onNewIntent(Intent intent) {
    String url = getUrlFromIntent(intent);
    if (!TextUtils.isEmpty(url))
      web.loadUrl(url);
  }

  @Override
  protected void onStart() {
    super.onStart();
    web.onStart();
  }

  @Override
  protected void onStop() {
    super.onStop();
  }

  @Override
  protected void onDestroy() {
    web.onDestroy();
    super.onDestroy();
  }

  private static String getUrlFromIntent(Intent intent) {
    return intent != null ? intent.getDataString() : null;
  }
}
